package slog

import (
	"ser1/database"
	"ser1/utils"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type OptionLogs struct {
	gorm.Model
	Name     string `json:"name" gorm:"type:varchar(100)" example:"John Doe"`
	UserId   uint   `json:"user_id" gorm:"not null;type:bigint" example:"1"`
	DataJson string `json:"data_json" gorm:"type:text" example:"{&quot;key&quot;:&quot;value&quot;}"`
}

func DB() *gorm.DB { return database.GetDB() }
func init() {
	database.AutoMigrateTables(&OptionLogs{})
}

// 添加日志
func AddOptionLogs(c *gin.Context, name string, data interface{}) {
	go func() {
		var op OptionLogs
		op.Name = name
		op.DataJson = utils.ToJson(data)
		op.UserId = utils.GetUserId(c)
		DB().Create(&op)
	}()

}
